﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Data;
using CMS;

namespace CMS
{
    public partial class ctrProductDetail : System.Web.UI.UserControl
    {
        public string cateId, productId, ps, strProductName;
        string sql = string.Empty;
        DataSet dsProduct;
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ctrProductDetail));

            try
            {
                cateId = Request.QueryString["CateId"].ToString();
            }
            catch
            {

                cateId = null;

            }
            try
            {
                productId = Request.QueryString["productId"].ToString();

            }
            catch
            {

                productId = null;
            }

            if (!IsPostBack)
            {
                if (Utilities.IsNullOrWhiteSpace(cateId))
                {
                    sql = "exec tblProduct_GetAllByCondition NULL";
                }
                else
                {
                    sql = string.Format("exec tblProduct_GetAllByCondition '{0}' ", " AND CategoryId=" + cateId + " AND ProductId =" + productId);

                }
                dsProduct = DAL.GetDataSet(sql);
                lblHTML.Text += Common.Utilities.AttachJavascript("Script", "jscript.js");
                lblHTML.Text += "<div id=\"Div2\" class=\"flower\" style=\"border-radius: 10px; -webkit-border-radius: 10px;-moz-border-radius: 10px; background-image: url('images/bg_head_product.jpg');background-repeat: repeat; height: 40px;\">";
                lblHTML.Text += "<h3>Sản phẩm " + strCateName() + "</h3></div>";
                lblHTML.Text += StrProduct(cateId, productId);
                lblHTML.Text += strLienquan();

            }
        }

        public string StrProduct(string cateId, string productId)
        {
            string strHtml = string.Empty;
            string s = string.Empty;

            strHtml += "<div id=\"Div1\" class=\"post\">";

            if (dsProduct != null && dsProduct.Tables[0].Rows.Count > 0)
            {
                DataRow dr = dsProduct.Tables[0].Rows[0];
                if (Convert.ToBoolean(dr["isPromotion"]) != true)
                {
                    dr["Promotion"] = string.Empty;
                }

                if (string.IsNullOrEmpty(dr["Image"].ToString()))
                {
                    dr["image"] = Common.Utilities.ImagesUrl() + "Nam-vang.PNG";
                }
                strHtml += "<div class=\"ClassPageDetail\">";
                strHtml += "  <table width=\"100%\" cellpadding=\"1\" cellspacing=\"1\"><tr>";
                strHtml += "<td  align=\"left\" valign=\"top\">";
                strHtml += "<table id=\"Table1\">";
                strHtml += "<tbody>";
                strHtml += "<tr>";
                strHtml += "<td colspan=\"2\" class=\"tdProductName\" height=\"30\">" + dr["ProductName"];
                strHtml += "            </td></tr><tr>";
                strHtml += "            <td valign=\"top\" >";
                strHtml += "            <a ><img src=" + dr["Image"] + " border=\"0\" width=\"240px\" height=\"200px\"></a>";
                strHtml += "            </td><td valign=\"top\" height=\"120px\">" + dr["ShortTitle"] + "</td>";
                strHtml += "            </tr><tr><td class=\"Descriptionproduct\">Giá bán:" + dr["RealPrice"] + "VNĐ</td>";
                strHtml += "            <td><a href= \"javascript:ClientAdd2ShoppingCartDetail(" + dr["ProductId"] + ")\"><img src=\"../images/cart.gif\" border=\"0\" />";
                strHtml += "            </a></td></tr><tr><td colspan=\"2\">";
                strHtml += "            <i style=\"color: rgb(255, 0, 0); font-family: verdana; font-size: 11px; font-variant: normal;font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto;text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);\">";
                strHtml += "            " + dr["Promotion"] + "</i>";
                strHtml += "</td></tr>";
                strHtml += "<tr><td colspan=\"2\">" + dr["Desciption"];
                strHtml += "</td></tr> </tbody></table></td></tr></table>";
                strHtml += "</div>";
            }
            strHtml += "</div>";

            return strHtml;
        }

        public string strCateName()
        {
            string sql = string.Format("exec tblCategory_GetAllByCondition '{0}' ", "AND CategoryId=" + cateId);
            var dsCate = DAL.GetDataSet(sql);
            if (dsCate != null && dsCate.Tables[0].Rows.Count > 0)
            {
                DataRow dr = dsCate.Tables[0].Rows[0];
                ps += ">>" + "<a style=\"text-decoration:none;color: #FFFFFF;\" href= \"Default.aspx?Cateid=" + dr["CategoryId"] + "\"" + ">" + dr["CategoryName"] + "</a> >>" + dsProduct.Tables[0].Rows[0]["ProductName"];

            }
            return ps;
        }

        public string strLienquan()
        {
            
            string strHtmlProduct = string.Empty;
            string Condition = "AND [ProductId] NOT IN (SELECT [ProductId] FROM [tblProduct] WHERE [ProductId]=" + productId + ")";
            string sql = string.Format("exec tblProduct_GetAllByCondition '{0}' ", "AND CategoryId=" + cateId + Condition);
            var dsProductByCate = DAL.GetDataSet(sql);
            if (dsProductByCate != null && dsProductByCate.Tables[0].Rows.Count > 0)
            {
                DataRow dr = dsProductByCate.Tables[0].Rows[0];

                if (Convert.ToBoolean(dr["isPromotion"]) != true)
                {
                    dr["Promotion"] = string.Empty;
                }

                if (string.IsNullOrEmpty(dr["Image"].ToString()))
                {
                    dr["image"] = Common.Utilities.ImagesUrl() + "Nam-vang.PNG";
                }
                strHtmlProduct += "<div id=\"Div2\" class=\"flower\" style=\"border-radius: 10px; -webkit-border-radius: 10px;-moz-border-radius: 10px; background-image: url('images/bg_head_product.jpg');background-repeat: repeat; height: 40px;\">";
                strHtmlProduct += "<h3>Sản phẩm liên quan</h3></div>";

                strHtmlProduct += "<div id=\"Div1\" class=\"post\">";
                strHtmlProduct += "<div class=\"ClassPage\">";
                strHtmlProduct += "  <table width=\"100%\" cellpadding=\"1\" cellspacing=\"1\"><tr>";
                strHtmlProduct += "<td  align=\"left\" valign=\"top\">";
                strHtmlProduct += "<table id=\"Table1\">";
                strHtmlProduct += "<tbody>";
                strHtmlProduct += "<tr>";
                strHtmlProduct += "<td colspan=\"2\" class=\"tdProductName\" height=\"30\">";
                strHtmlProduct += "<a href= Default.aspx?Cateid=" + dr["CategoryId"] + "&ProductId=" + dr["ProductId"] + ">" + dr["ProductName"] + "</a>";
                strHtmlProduct += "            </td></tr><tr>";
                strHtmlProduct += "            <td valign=\"top\" width=\"120px\" height=\"120px\">";
                strHtmlProduct += "            <a href=Default.aspx?Cateid=" + dr["CategoryId"] + "&ProductId=" + dr["ProductId"] + "><img src=" + dr["Image"] + " border=\"0\"></a>";
                strHtmlProduct += "            </td><td valign=\"top\" height=\"120px\">" + dr["ShortTitle"] + "</td>";
                strHtmlProduct += "            </tr><tr><td class=\"Descriptionproduct\">Giá bán:" + dr["RealPrice"] + "VNĐ</td>";
                strHtmlProduct += "            <td><a href= \"#\"><img src=\"../images/cart.gif\" border=\"0\" />";
                strHtmlProduct += "            </a></td></tr><tr><td colspan=\"2\">";
                strHtmlProduct += "            <i style=\"color: rgb(255, 0, 0); font-family: verdana; font-size: 11px; font-variant: normal;font-weight: normal; letter-spacing: normal; line-height: 20px; orphans: 2; text-align: -webkit-auto;text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);\">";
                strHtmlProduct += "            " + dr["Promotion"] + "</i>";
                strHtmlProduct += "</td></tr></tbody></table></td></tr></table>";
                strHtmlProduct += "</div>";
                strHtmlProduct += "</div>";

            }
            return strHtmlProduct;
        }

        [AjaxPro.AjaxMethod]
        public static string ServerSideAddShoppingCart(int ProductId)
        {
            try
            {
                ShopCart.Add2Cart(ProductId);
                return Common.ResourceBLL.GetFrontEndText("Vi", "ctrProduct", "sp", "Sản phẩm bạn đặt đã đưa vào giỏ hàng!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}